
import { store } from '@/stores';
import { defineStore } from 'pinia';

interface UserState {
    userInfo: any | null;
    token: string | undefined;
    sys: any | null;
    currentSystemName: String | undefined;
    currentSystemCode: String | undefined;
}

export const useUserStore = defineStore({
    id: 'app-user',
    state: (): UserState => ({
        //用户信息
        userInfo: null,
        //token
        token: undefined,
        //系统信息
        sys: null,
        //当前系统名称
        currentSystemName: '',
        //当前系统代码
        currentSystemCode: '',
    }),
    getters: {
        getUser(): any {
            return this.userInfo || JSON.parse(uni.getStorageSync("userInfo") || '{}') || {};
        },
        getSys(): any {
            return this.sys || JSON.parse(uni.getStorageSync("sys") || '{}') || {};
        },
        getCurrentSystemName(): string {
            return this.currentSystemName || uni.getStorageSync("currentSystemName") || '数据世界'
        },
        getCurrentSystemCode(): string {
            return this.currentSystemCode || uni.getStorageSync("currentSystemCode") || 'KFZFW'
        },
        getToken(): string {
            return this.token || uni.getStorageSync("token") || ''
        }
    },
    actions: {
        userAuth(auth: string) {
            return this.getUser.qxMap[auth]
        },
        /**
         * 设置用户信息
         * @param userInfo
         */
        setUser(userInfo: any | null) {
            this.userInfo = userInfo;
            if (userInfo) {
                uni.setStorageSync('userInfo', JSON.stringify(userInfo));
            } else {
                uni.removeStorageSync("userInfo")
            }
        },

        /**
         * 设置系统信息
         * @param sys
         */
        setSys(sys: any) {
            this.sys = sys;
            if (sys) {
                uni.setStorageSync("sys", JSON.stringify(sys))
            }
            else {
                uni.removeStorageSync("sys")
            }
        },

        /**
         * 设置token
         * @param token
         */
        setToken(token: string | undefined) {
            this.token = token
            if (token)
                uni.setStorageSync("token", token)
            else {
                uni.removeStorageSync("token")
            }
        },

        /**
         * 设置当前系统名称
         * @param name
         */
        setCurrentSystemName(name: string | undefined) {
            this.currentSystemName = name
            if (name) {
                uni.setStorageSync("currentSystemName", name)
            }
            else {
                uni.removeStorageSync("currentSystemName")
            }
        },

        /**
         * 设置当前系统代码
         * @param code
         */
        setCurrentSystemCode(code: string | undefined) {
            this.currentSystemCode = code
            if (code)
                uni.setStorageSync("currentSystemCode", code)
            else {
                uni.removeStorageSync("currentSystemCode")
            }

        }
    },
})

// Need to be used outside the setup
export function useUserStoreWithOut() {
    return useUserStore(store);
}
